<template>
  <div class="supplierOrder4Deliver" style="padding-bottom: 50px">
    <el-row type="flex" justify="center">
      <div class="input_box">
        <el-input
            v-model="selectInput"
            placeholder="请输入查询内容"
            size="small"
            clearable
        >
          <template #append>
            <el-button @click="search()" size="small">
              <el-icon>
                <Search/>
              </el-icon>
            </el-button>
          </template>
        </el-input>
      </div>
      <el-card>
        <el-table
            :data="orderData"
            class-name="orderTable"
            :header-cell-style="{
              'text-align':'center',
              'font-size':'19px',
              'font-weight':'normal',
              'border-color': 'black'
            }"
            :cell-style="{
              'text-align':'center',
              'height':'30px',
              'font-size':'16px',
              'background-color':'pink',
              'border-color':'black'
            }"
            border
        >
          <el-table-column type="expand" label="订单详情" width="130px">
            <template #default="orderDetails">
              <div class="table">
                <h5>&nbsp;Details</h5>
                <el-table
                    :data="orderDetails.row.orderItemList"
                    class-name="orderItemsTable"
                    :header-cell-style="{
                    'font-size':'17px',
                    'font-weight':'normal',
                  }"
                    :cell-style="{
                    'font-size':'16px',
                  }"
                    :row-class-name="tableRowClassName"
                    style="width: 70%"
                >
                  <el-table-column prop="goodsImg" label="商品图片">
                    <template v-slot="scope">
                      <img :src="scope.row.goodsImg" min-width="70px" height="80px"/>
                    </template>
                  </el-table-column>
                  <el-table-column prop=goodsName label="商品名称"/>
                  <el-table-column prop="goodsCount" label="数量">
                    <template v-slot="scope">
                      <el-tag size="small" type="success" effect="light">
                        {{ scope.row.goodsCount }}
                      </el-tag>
                    </template>
                  </el-table-column>
                  <el-table-column prop="goodsUnitPrice" label="单价"/>
                </el-table>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="orderTime" label="下单时间" width="180px" :formatter="formatter"/>
          <el-table-column prop="purchaserNickname" label="采购商" width="150px"/>
          <el-table-column prop="purchaserAccount" label="联系方式" width="180px"/>
          <el-table-column prop="purchaserAddress" label="地址" width="250px"/>
          <el-table-column prop="realPayment" label="实付款" width="130px"/>
          <el-table-column prop="controls" label="操作" width="190px" fixed="right">
            <template v-slot="scope">
              <el-button size="small"
                         type="primary"
                         plain
                         @click="sendOrder(scope.row.orderId, scope.row.realPayment, scope.row.orderItemList)"
              >
                发货
              </el-button>
              <el-popconfirm
                  confirm-button-text="确认"
                  cancel-button-text="取消"
                  :icon="InfoFilled"
                  icon-color="#626aef"
                  title="是否确认取消该笔交易？"
                  @confirm="cancelOrder(scope.row.orderId, scope.row.purchaserId, scope.row.realPayment, scope.row.orderItemList)"
                  width="300px"
                  popper-class="popper1"
              >
                <template #reference>
                  <el-button size="small" type="danger" plain>
                    取消交易
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-row>
  </div>
</template>

<script setup name="PurchaserOrder">
import {InfoFilled, Search} from "@element-plus/icons-vue";
import {ref, reactive, onMounted} from "vue"
import request from "@/utils/request";
import {ElMessage} from "element-plus";

const selectInput = ref('')

const orderData = reactive([])

onMounted(() => {
  selectAllDeliverOrder()
})


function tableRowClassName({rowIndex}) {
  if (rowIndex % 2 == 0) {
    return "";
  } else {
    return "warning-row";
  }
}

function selectAllDeliverOrder() {
  let params = {
    selectInput: selectInput.value ? selectInput.value : null
  }
  request.get("/t-order/getSupplierDeliverOrder", {
    params: params
  }).then((resp) => {
    orderData.length = 0
    orderData.push(...resp.data)
  })
}

function search() {
  selectAllDeliverOrder()
}

function cancelOrder(oId, purchaserId, realPayment, orderItemList) {
  request.put(`/supplier/orderRefund/${oId}/${purchaserId}/${realPayment}`, orderItemList).then(() => {
    selectAllDeliverOrder()
    ElMessage({
      message: '取消交易成功',
      type: 'success',
      plain: true
    })
  })
}

function sendOrder(oId, realPayment, orderItemList) {
  request.put(`/supplier/orderCompleted/${oId}/${realPayment}`, orderItemList).then(() => {
    selectAllDeliverOrder()
    ElMessage({
      message: '发货成功',
      type: 'success',
      plain: true
    })
  })
}

function formatter(row, column) {
  let newTime = row.orderTime.replace("T", " ")
  return newTime
}
</script>

<style scoped>
.el-card {
  margin-top: 6px;
  width: 580px;
  background-color: pink;
  align: center;
}

.orderTable {
  border: 1px solid black;
}

.el-button {
  font-size: 9px;
  padding: 2px 10px;
  margin-left: -5px;
  margin-right: -5px;
}

.input_box {
  width: 300px;
  border-radius: 95px;
  background: #57d1e6;
  margin-top: 6px;
}

:deep(.el-input__wrapper) {
  background-color: transparent;
  border-radius: 95px;
  border: 0px;
  box-shadow: 0 0 0 0px;
  font-size: 11px;
}

:deep(.el-input-group__append) {
  background: #57d1e6;
  border-radius: 95px;
  border: 0px;
  box-shadow: 0 0 0 0px;
}

.orderItemsTable {
  padding: 10px;
}

/*
:deep(.el-table__row.warning-row) {
  background: #d1e9f4;
}
*/
</style>

<style>
.popper1 {
  .el-popconfirm__main {
    font-size: 11px;
  }

  .el-button--small {
    font-size: 9px !important;
    width: 30px;
    height: 20px;
  }
}
</style>
